package cn.schoolwow.quickdao.module.database.ddl.action.service.table;

import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.domain.entity.common.TableColumn;
import cn.schoolwow.quickdao.domain.entity.create.CreateTableRequest;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.beanCopy.BeanCopy;

import java.util.ArrayList;
import java.util.List;

public class SetEntityByCreateTableRequestFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        CreateTableRequest createTableRequest = flowContext.checkInstanceData(CreateTableRequest.class);

        Entity entity = new Entity();
        entity.tableName = createTableRequest.tableName;
        entity.comment = createTableRequest.comment;

        List<Property> propertyList = new ArrayList<>();
        for(TableColumn tableColumn:createTableRequest.columnList){
            Property property = BeanCopy.cloneObject(tableColumn, Property.class);
            property.strategy = tableColumn.strategy;
            propertyList.add(property);
        }
        entity.properties = propertyList;
        flowContext.putTemporaryData("entity", entity);
    }

    @Override
    public String name() {
        return "根据CreateTableRequest对象设置Entity对象";
    }
}
